package com.common.utils
{
	import com.greensock.TweenLite;
	
	import flash.display.DisplayObject;
	import flash.display.GraphicsPathCommand;
	import flash.display.Shape;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.net.registerClassAlias;
	import flash.utils.ByteArray;
	import flash.utils.Endian;
	import flash.utils.Timer;
	import flash.utils.getDefinitionByName;
	import flash.utils.getQualifiedClassName;

	/**
	 * Object 的一些经常使用的function
	 * @author lake
	 * 
	 */	
	public class ObjectUtil
	{
		public function ObjectUtil()
		{
		}
		/**
		 * 深复制
		 * @param value
		 * @return 
		 * 
		 */		
		public static function deepCopy(value:Object):*
		{
			if(value == null)
			{
				return null;
			}
			var qClassName:String = getQualifiedClassName(value);  
			var objectType:Class = getDefinitionByName(qClassName) as Class;  
			registerClassAlias(qClassName, objectType);
			var copier : ByteArray = new ByteArray();  
			copier.writeObject(value);  
			copier.position = 0;  
			return copier.readObject(); 
		}
		/**
		 *获取一个新的ByteArray 
		 * @return 
		 * 
		 */		
		public static function getByteArray():ByteArray
		{
			var ba :ByteArray = new ByteArray();
			ba.endian = Endian.LITTLE_ENDIAN;
			
			return ba;
		}
		/**
		 *绘制能力图形 
		 * @param pots
		 * @return 
		 * 
		 */		
		public static function drawPowerGraphics(powers :Array,color :uint = 0xff0000,alpha :Number = 1):Shape
		{
			var pots :Vector.<Point> = new Vector.<Point>();
			var pot :Point;
			var radians :Array = [Math.PI/6,Math.PI/2,Math.PI*5/6,Math.PI*7/6,Math.PI*9/6,Math.PI*11/6];
			var i :int = 0;
			i = 0;
			var radio :Number = 30 / 120;
			for each(var power :int in powers)
			{
				pot = new Point();
				pot.x = (power * radio + 15) * Math.cos(radians[i]);
				pot.y = - (power * radio + 15) * Math.sin(radians[i]);
				pots.push(pot);
				i ++ ;
			}
			var shape :Shape = new Shape();
			var command:Vector.<int> = new Vector.<int>();
			var value:Vector.<Number> = new Vector.<Number>();
			command.push(GraphicsPathCommand.MOVE_TO);
			pot = pots[0];
			value.push(pot.x, pot.y);
			for each(pot in pots)
			{
				command.push(GraphicsPathCommand.LINE_TO);
				value.push(pot.x, pot.y);
			}
			command.push(GraphicsPathCommand.LINE_TO);
			pot = pots[0];
			value.push(pot.x, pot.y);
			
			shape.graphics.beginFill(color,alpha);
			shape.graphics.drawPath(command, value);
			
			return shape;
		}
		//==============================================================
		//==============================================================
		private static var _shake_init_x:Number;
		private static var _shake_init_y:Number;
		private static var _target:DisplayObject;
		private static var _maxDis:Number;
		private static var _count:int = 0;
		private static var _rate:Number;
		/**
		 * 震动显示对象
		 * @param        target                震动目标对象
		 * @param        time                        震动持续时长（秒）
		 * @param        rate                        震动频率(一秒震动多少次)
		 * @param        maxDis        震动最大距离
		 * shakeObj(this, .5, 60, 10);
		 */
		public static function shakeObj(target:DisplayObject , time:Number ,rate:Number, maxDis:Number):void
		{
			_target                         = target;
			_shake_init_x         = target.x;
			_shake_init_y         = target.y;
			_maxDis                         = maxDis;
			_count                                 = time * rate;
			_rate                                 = rate;
			
			var timer:Timer = new Timer(1000 / rate, _count );
			timer.addEventListener(TimerEvent.TIMER, shaking);
			timer.addEventListener(TimerEvent.TIMER_COMPLETE, shakeComplete);
			timer.start();
		}
		
		static private function shaking(e:TimerEvent):void 
		{
			TweenLite.killTweensOf(_target);
			_target.x         = _shake_init_x +Math.random() * _maxDis;
			_target.y        = _shake_init_y +Math.random() * _maxDis;
			TweenLite.to( _target,999 / _rate, { x : _shake_init_x, y:_shake_init_y } );
		}
		
		static private function shakeComplete(e:TimerEvent):void 
		{
			TweenLite.killTweensOf(_target);
			_target.x         = _shake_init_x;
			_target.y         = _shake_init_y;
		}
	}
}